<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <style>
        body {
            background-color: #3498db; /* 蓝色背景 */
            color: #fff; /* 白色文字 */
            font-family: 'Arial', sans-serif;
        }

        h2 {
            color: #e74c3c; /* 粉色标题 */
        }

        form {
            background-color: #fff; /* 白色背景 */
            padding: 20px;
            border-radius: 10px;
            margin-top: 20px;
        }

        input[type="file"] {
            margin-bottom: 10px;
        }

        /* 按钮样式 */
        button {
            background-color: #e74c3c; /* 粉色按钮 */
            color: #fff;
            padding: 10px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease; /* 添加过渡效果 */
        }

        button:hover {
            background-color: #c0392b; /* 鼠标悬停时的颜色 */
        }

        /* 进度条样式 */
        progress {
            width: 100%;
            height: 20px;
            margin-bottom: 10px;
            border-radius: 5px;
            overflow: hidden; /* 隐藏边框圆角溢出部分 */
            background-color: #ecf0f1; /* 进度条背景色 */
        }

        progress::-webkit-progress-bar {
            background-color: #ecf0f1; /* WebKit 浏览器的背景色 */
            border-radius: 5px;
        }

        progress::-webkit-progress-value {
            background-color: #3498db; /* WebKit 浏览器的进度条颜色 */
            border-radius: 5px;
        }

        progress::-moz-progress-bar {
            background-color: #3498db; /* Firefox 浏览器的进度条颜色 */
            border-radius: 5px;
        }

        p {
            margin-top: 10px;
        }
        /* 自定义文件选择按钮的样式 */
        .file-input {
            display: none; /* 隐藏默认的文件选择按钮 */
        }

        .custom-file-input {
            display: inline-block;
            background-color: #3498db; /* 蓝色背景 */
            color: #fff; /* 白色文字 */
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .custom-file-input:hover {
            background-color: #2980b9; /* 鼠标悬停时的颜色 */
        }

        #uploadFail {
        color: #e74c3c; /* 红色文字 */
        font-weight: bold;
        margin-top: 10px;
        padding: 10px;
        border: 2px solid #e74c3c; /* 添加红色边框 */
        border-radius: 5px; /* 圆角边框 */
        background-color: #f2dede; /* 浅红色背景 */
    }
    /* 按钮样式 */
    .login-btn {
            display: inline-block;
            padding: 10px 15px;
            background-color: #3498db; /* 蓝色背景 */
            color: #fff; /* 白色文字 */
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease; /* 添加过渡效果 */
        }

        .login-btn:hover {
            background-color: #2980b9; /* 鼠标悬停时的颜色 */
        }

    </style>
</head>
<body>

<h2>文件上传</h2>

<!-- 文件上传表单 -->
<form id="uploadForm" enctype="multipart/form-data">
    <!-- 使用自定义样式的文件选择按钮 -->
    <label for="fileToUpload" class="custom-file-input">选择文件</label>
    <br>
    <input type="file" name="fileToUpload" id="fileToUpload" class="file-input">
    <br>
    <progress id="uploadProgress" value="0" max="100"></progress>
    <br>
    <button type="button" onclick="startUpload()">上传文件</button>
</form>

<!-- 上传状态 -->
<p id="uploadStatus"></p>

<!-- 跳转到登录界面的按钮 -->
<a href="login.html" class="login-btn">跳转到登录界面</a>
<script>
    function startUpload() {
        const fileInput = document.getElementById('fileToUpload');
        const uploadForm = document.getElementById('uploadForm');
        const uploadProgress = document.getElementById('uploadProgress');
        const uploadStatus = document.getElementById('uploadStatus');

        const formData = new FormData(uploadForm);

        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'upload.php', true);

        xhr.upload.addEventListener('progress', (event) => {
            if (event.lengthComputable) {
                const progress = Math.round((event.loaded / event.total) * 100);
                uploadProgress.value = progress;
            }
        });

        xhr.addEventListener('readystatechange', () => {
    if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status === 200) {
            const response = JSON.parse(xhr.responseText);
            uploadStatus.innerText = `上传成功：${response.fileName}`;
        } else {
            uploadStatus.innerText = '上传失败';
            console.error(`上传失败：${xhr.statusText}`);
        }
    }
});


        xhr.send(formData);

        // 每秒钟轮询一次服务器，获取实时进度
        setInterval(() => {
            fetch('get_progress.php')
                .then(response => response.json())
                .then(data => {
                    const progress = data.progress;
                    if (progress !== undefined) {
                        uploadProgress.value = progress;
                    }
                })
                .catch(error => {
                    console.error('获取进度失败：', error);
                });
        }, 1000);
    }
</script>

</body>
</html>
